package xxy.com.customview;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.AnimatorSet;
import android.animation.ArgbEvaluator;
import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.app.ActivityOptions;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.constraint.ConstraintLayout;
import android.support.constraint.ConstraintSet;
import android.support.transition.TransitionManager;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import xxy.com.customview.util.UIUtils;

public class Main3Activity extends AppCompatActivity {
    @BindView(R.id.main3_toolBar)
    Toolbar toolBar;
    @BindView(R.id.mian3_iv_women)
    ImageView ivWomen;
    @BindView(R.id.main3_cl)
    ConstraintLayout cl;

    ConstraintSet constraintSet = new ConstraintSet();
    @BindView(R.id.main3_bt)
    Button main3Bt;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main3);
        ButterKnife.bind(this);
        init();
        initView();
        setClickListener();
    }

    public void init() {
        setSupportActionBar(toolBar);
    }

    private void initView() {
        constraintSet.clone(cl);   //把ConstraintLayout赋值给ConstraintSet
//        animation();
    }

    private void setClickListener() {
        ivWomen.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setViewsNewLayout();
                //宽和高设为100
                constraintSet.constrainWidth(R.id.mian3_iv_women, UIUtils.dp2Px(100, Main3Activity.this));
                constraintSet.constrainHeight(R.id.mian3_iv_women, UIUtils.dp2Px(100, Main3Activity.this));
                constraintSet.applyTo(cl);   //把ConstraintSet改变的参数赋值给ConstraintSetConstraintLayout

                //延迟0.5秒进行动画
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        animation();
                    }
                }, 500);
            }
        });

    }

    /**
     * 设置新的布局位置
     */
    private void setViewsNewLayout() {
        TransitionManager.beginDelayedTransition(cl);   //实现弹性动画效果
        //边距重置为0
        constraintSet.setMargin(R.id.mian3_iv_women, ConstraintSet.START, 0);
        constraintSet.setMargin(R.id.mian3_iv_women, ConstraintSet.END, 0);
        constraintSet.setMargin(R.id.mian3_iv_women, ConstraintSet.TOP, 0);
        constraintSet.setMargin(R.id.mian3_iv_women, ConstraintSet.BOTTOM, 0);
        //设置父布局中心位置
        constraintSet.centerHorizontally(R.id.mian3_iv_women, R.id.main3_cl);
        constraintSet.centerVertically(R.id.mian3_iv_women, R.id.main3_cl);
    }

    private void animation() {
        //属性动画
        //1、
//        ObjectAnimator translationY = ObjectAnimator.ofFloat(ivWomen, "translationY", -ivWomen.getHeight());
//        translationY.setDuration(1000);
//        translationY.setInterpolator(new DecelerateInterpolator());
//        translationY.start();

//        ObjectAnimator translationY1 = ObjectAnimator.ofFloat(ivWomen, "translationY", ivWomen.getHeight());
//        translationY1.setDuration(1000);
//        translationY1.setInterpolator(new BounceInterpolator());
//        translationY1.start();
        //2、变换颜色
        ValueAnimator backgroundColor = ObjectAnimator.ofInt(cl, "backgroundColor", 0xFFF8080, 0xFF8080FF);
        backgroundColor.setDuration(3000);
        backgroundColor.setEvaluator(new ArgbEvaluator());
        backgroundColor.setRepeatCount(ValueAnimator.INFINITE);
        backgroundColor.setRepeatMode(ValueAnimator.REVERSE);
        backgroundColor.start();
        //3、属性动画集合
        AnimatorSet animatorSet = new AnimatorSet();
        animatorSet.playTogether(ObjectAnimator.ofFloat(ivWomen, "rotationX", 0, 360),
                ObjectAnimator.ofFloat(ivWomen, "rotationY", 0, 180),
                ObjectAnimator.ofFloat(ivWomen, "translationX", 0, 400),
                ObjectAnimator.ofFloat(ivWomen, "translationY", 0, -200),
                ObjectAnimator.ofFloat(ivWomen, "alpha", 1, 0.25f, 1),
                ObjectAnimator.ofFloat(ivWomen, "scaleX", 1, 1.5f),
                ObjectAnimator.ofFloat(ivWomen, "scaleY", 1, 1.5f));
        animatorSet.setDuration(5 * 1000).start();
        //动画监听，动画完成后，放大图片
        animatorSet.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                super.onAnimationEnd(animation);
                setViewsNewLayout();
                constraintSet.constrainWidth(R.id.mian3_iv_women, UIUtils.dp2Px(500, Main3Activity.this));
                constraintSet.constrainHeight(R.id.mian3_iv_women, UIUtils.dp2Px(500, Main3Activity.this));
                constraintSet.applyTo(cl);   //把ConstraintSet改变的参数赋值给ConstraintSetConstraintLayout
            }
        });
    }

    @OnClick({R.id.main3_bt ,R.id.main3_mybtn_back })
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.main3_bt:
                startActivity(new Intent(Main3Activity.this, Main4Activity.class), ActivityOptions.makeSceneTransitionAnimation(Main3Activity.this).toBundle());
                break;
            case R.id.main3_mybtn_back:
                onBackPressed();
                break;
        }
    }
}
